/*
 * BulletBaseVehicle.h
 * BulletBaseVehicle class interface
 */

#pragma once

#include "orbitersdk.h"

//useful macros
#define V3ARGS(a)	(a).x, (a).y, (a).z

struct KeyPressInfo{
	DWORD key;
	bool down;
	char *kstate;
};

/**
 * ==============================================================
 * This class implements an attachment to a BulletBase modeling a vehicle
 * TODO: It should be derived from a BulletBaseAttachment class
 * ==============================================================
 */

class BulletBaseVehicle: public VESSEL3 {
public:
	BulletBaseVehicle (OBJHANDLE hVessel, int flightmodel);
	virtual ~BulletBaseVehicle ();

	/* VESSEL class overrides */
	void clbkSetClassCaps (FILEHANDLE cfg);
	void clbkLoadStateEx (FILEHANDLE scn,  void * status);
	void clbkPreStep(double SimT, double SimDT, double MJD);
	int  clbkConsumeBufferedKey(DWORD key, bool down, char *kstate);
	int	 clbkGeneric(int  msgid, int  prm , void *context);
	void clbkSaveState(FILEHANDLE  scn);
	//void clbkFocusChanged(bool  getfocus, OBJHANDLE  hNewVessel, OBJHANDLE  hOldVessel);

	/* Utility stuff */
	void attachShuttle(void);
	void detachShuttle();
	void updateBase(void);

	/* Wheel Animation stuff */
	void loadMeshFileDefaultNames(FILEHANDLE cfg);
	void initAnimations();
	void runAnimations(struct packet *pkt);

	//Incoming stuff from bullet
	VECTOR3 local[4];
	float steering, speed;
	float wheelRot[4];
	ATTACHMENTHANDLE athMineWithBase;
	bool updateVessel;

	//position wrt base
	VECTOR3 pos;
	float yaw, pitch, roll;

	//Outgoing stuff
	bool validKeyInput, physicsPositionAvailable;
	int gear;
	KeyPressInfo keyInput;
	float bearing;

	/* BulletBase stuff */
	char baseName[100];
	VESSEL3 *vBase;

	/* Shuttle specific stuff */
	ATTACHMENTHANDLE athParentShuttle, athChildShuttle;
	VESSEL3 *vShuttle;
	char shuttleName[100];
	bool shuttleAttached;

	OBJHANDLE hobjBBV, hobjShuttle;
	bool baseInited, meshesLoaded;
	char mshChassisName[100], mshWheelLeftForwardName[100], mshWheelRightForwardName[100];
	char mshWheelLeftRearName[100], mshWheelRightRearName[100];
	char physicsFile[100];

	//Engine parameters
	float	maxEngineForce;
	float	maxBrakingForce;
	float 	reverseEngineForce;
	float	mass;

	//Transmission : simple gear model which varies engine force only
	int 	maxGear; //Reverse and neutral indicated by 'R' & 'N'
	int 	minGear;	

	//Steering parameters
	float	steeringIncrement;
	float	steeringClamp;

	// Wheel parameters
	float	wheelRadius;
	float	wheelWidth;	 //actual width is 2.364(twice)
	float	wheelFriction;
	float	wheelFwdBackDist;
	float   wheelLeftRightDist;
	float	connectionHeight;

	//Suspension parameters
	float	suspensionStiffness;
	float	suspensionDamping;
	float	suspensionCompression;
	float	rollInfluence;
	float 	suspensionRestLength;

	//Heights and initial location
	float 	initialVehicleHeight; //Height from which vehicle is dropped on terrain at physics start
	float 	chassisClearanceHeight; //Height of chassis above vehicle origin
	float 	chassisLength;
	float 	chassisWidth;
	float 	chassisHeight;


private:

	char *name;

	/* Animation stuff */
	MGROUP_TRANSLATE *mgtTX[4], *mgtTY[4], *mgtTZ[4];
	MGROUP_ROTATE *mgrRX[4], *mgrRY[4];
	UINT animTX[4], animTY[4], animTZ[4], animRX[4], animRY[4];
	//VECTOR3 local, global;



};
